<?php

$pagestartime=microtime(); 

$time_log='';

include 'func.inc.php';

$w = 6;     // 宽
$h = 6;     // 高
$density = 55;    // 密度
$max = 1000;
$debugMode = 0;

$tt = intval(v('debug'))  ;
if ($tt>0 ){
    $debugMode=1;
}

$tt = intval(v('w'))  ;
if ($tt>0 && $tt<=$max){
    $w=$tt;
}else{
    die('403 Access Denied/Forbidden');
}

$tt = intval(v('h'))  ;
if ($tt>0 && $tt<=$max){
    $h=$tt;
}else{
    die('403 Access Denied/Forbidden');
}

$tt = intval(v('d'))  ;
if ($tt>0 && $tt<100){
    $density=$tt;
}

require 'kisRandomMap.php';
require 'AStar.php';

$ksRndMap = new kisRandomMap();
// $aStar = new AStar();


$endId = ($w * $h)-1;

// $time_log.= "基础加载时间: ". used_time($pagestartime) ."秒.<br/>".PHP_EOL; 
$time1=microtime(); 

$ksRndMap->set($w, $h);
$ksRndMap->create();
// $grids = $ksRndMap->get();
$ksRndMap->setDensity($density);
// $ksRndMap->setCA(4,5);
$ksRndMap->run_ca();
$grids = $ksRndMap->get();

if($debugMode) $ksRndMap->set_debug(1);
if($debugMode) echo '<!--';
$tmpNodeSet=$ksRndMap->debug_walk();
if($debugMode) echo '-->';
$time_log.= "maze->create 时间: ". used_time($time1) ."秒.<br/>".PHP_EOL; 
$time2=microtime(); 

// // 寻路
// $aStar->set($w, $h, $grids);
// $path = $aStar->search(0, $endId);       // 从零开始找到35
$path=$tmpNodeSet;
// $time_log.= "aStar->search 时间: ". used_time($time2) ."秒.<br/>".PHP_EOL; 

function _rnd($min,$max) {
    return mt_rand($min,$max);
}

//画地图
function map_div($x, $y, $v){
    global $w, $h, $path;

    $rd = 149;

    //if ( $y > 4 ) $v = 0;
    $k = $y * $w + $x;
    //if ( array_key_exists($k, $this->_enable) ) {
        //echo "<div class=\"grid cell_enable_{$this->_enable[$k]}\" style=\"";
    //} else {
        echo "<div class=\"grid tileBpic ";
        if($v==='*') {
            //70%是草地,其他事花朵
            $catArr=array(161,162,163,164,177,178,179,180,216,232,243);
            $jj = _rnd(0,100);
            if($jj<70){
                $tt = 2;
            }else{
                $tt = _rnd(0,count($catArr)-1);
            }            
            echo 'tileBpic_'.$catArr[$tt];
        }else{
            $catArr=array(89,181,168,184);  //树,枯木,石,矿
            $jj = _rnd(0,100);
            if($jj<90){
                $tt = 0;
            }else if($jj<95){
                $tt = 1;
            }else{                
                $tt = _rnd(0,count($catArr)-1);
            }
            echo 'tileBpic_'.$catArr[$tt];
        }        
        echo "\" style=\"";
    //}
    echo 'top:'. 35*$y.'px;';
    echo 'left:'. 35*$x.'px;';
    //if ($v & 1)
        //echo "border-top:1px solid #F5F5F5;";
    //if ($v & 2)
        //echo "border-right:1px solid #F5F5F5;";
    //if ($v & 4)
        //echo "border-bottom:solid 1px #F5F5F5;";
    //if ($v & 8)
        //echo "border-left:solid 1px #F5F5F5;";
    if ( in_array($y * $w + $x, $path) )
        echo "color:red;";
    echo '">';
    // echo $y * $w + $x;
    // if($v==='*') {
    //     echo '.';
    //     //163
    // }else{
    //     echo '#';
    //     //89
    // }
    echo '</div>'."\n";
}

//画地图
function div2($x, $y, $v){
    global $w, $h, $path;
    //if ( $y > 4 ) $v = 0;
    $k = $y * $w + $x;
    //if ( array_key_exists($k, $this->_enable) ) {
        //echo "<div class=\"grid cell_enable_{$this->_enable[$k]}\" style=\"";
    //} else {
        echo "<div class=\"grid \" style=\"";
    //}
    echo 'top:'. 10*$y.'px;';
    echo 'left:'. 10*$x.'px;';
    //if ($v & 1)
        //echo "border-top:1px solid #F5F5F5;";
    //if ($v & 2)
        //echo "border-right:1px solid #F5F5F5;";
    //if ($v & 4)
        //echo "border-bottom:solid 1px #F5F5F5;";
    //if ($v & 8)
        //echo "border-left:solid 1px #F5F5F5;";
    if ( in_array($y * $w + $x, $path) )
        echo "color:red;";
    echo '">';
    // echo $y * $w + $x;
    if($v==='*') {
        echo '.';
        //163
    }else{
        echo '#';
        //89
    }
    echo '</div>'."\n";
}

// 画迷宫方法
function div($x, $y, $v){
    global $w, $h, $path;
    //if ( $y > 4 ) $v = 0;
    $k = $y * $w + $x;
    //if ( array_key_exists($k, $this->_enable) ) {
        //echo "<div class=\"grid cell_enable_{$this->_enable[$k]}\" style=\"";
    //} else {
        echo "<div class=\"grid cell_{$v}\" style=\"";
    //}
    echo 'top:'. 34*$y.'px;';
    echo 'left:'. 34*$x.'px;';
    //if ($v & 1)
        //echo "border-top:1px solid #F5F5F5;";
    //if ($v & 2)
        //echo "border-right:1px solid #F5F5F5;";
    //if ($v & 4)
        //echo "border-bottom:solid 1px #F5F5F5;";
    //if ($v & 8)
        //echo "border-left:solid 1px #F5F5F5;";
    // if ( in_array($y * $w + $x, $path) )
    //     echo "color:red;";
    $len = count($path);
    $colorArr=array('#66CCCC','#CCFF66','#FF99CC','#FFCC00','#009999','#CC3333','#666699','#CCFF99','#FF9900');
    foreach ($path as $tmpid=>$tmpArr) {
        if ($tmpArr)
        if ( in_array($y * $w + $x, $tmpArr) ){
            $id = fmod($tmpid,count($colorArr));
            echo "background-color:".$colorArr[$id].';'; 
            break;
        }
    }
    echo '">';
    echo $y * $w + $x;
    // echo $v;
    echo '</div>'."\n";
}


function used_time($pagestartime){
	$pageendtime = microtime();
	$starttime = explode(" ",$pagestartime); 
	$endtime = explode(" ",$pageendtime); 
	$totaltime = $endtime[0]-$starttime[0]+$endtime[1]-$starttime[1]; 
	$timecost = sprintf("%s",$totaltime); 
	// echo "页面运行时间: $timecost 秒"; 
	// echo "页面运行时间: ". used_time() ."秒"; 
	return $timecost;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<title><?php echo 'W'.$w.',H'.$h; ?></title>
<link href="css/map.css" rel="stylesheet" type="text/css">
<link rel='stylesheet' type='text/css' href='http://shdic1.duapp.com/apps/sheepy_arena/html/res/TileB.css?ver=20120819_mod20140930'>
</head>
<body>
<?php
if($debugMode) {
    echo '<!--'.PHP_EOL;
    $baseNodeSet=$ksRndMap->debug_nodeSet();
    foreach ($baseNodeSet as $tmpSetId => $tmpSetArr) {
            echo '#'.$tmpSetId.'='.implode(',',$tmpSetArr) .PHP_EOL;    
    }
    echo '========================================='.PHP_EOL;
    foreach ($tmpNodeSet as $tmpSetId => $tmpSetArr) {
            echo '#'.$tmpSetId.'='.implode(',',$tmpSetArr) .PHP_EOL;    
    }
    echo '-->'.PHP_EOL;
}
?>
<div style="margin:20px auto;" class="mg">
<?php
for($y = 0; $y < $h; $y++ ){
    for($x=0; $x < $w; $x++ ){
        $v = $grids[$y * $w + $x];
        if($debugMode) {
            div($x, $y, $v);
        }else{
            map_div($x, $y, $v);
        }
        
    }
}
?>
</div>
<?php
echo '<!--'.PHP_EOL;
$time_log.= "页面运行时间: ". used_time($pagestartime) ."秒.<br/>".PHP_EOL; 
echo $time_log;
?>
-->
</body>
</html>